<?php
/**
 * 微信公众号设置互动Controller
 */

namespace backend\controllers;


use backend\commands\BaseAdminController;
use common\models\DAO\NewsPush;
use common\models\Service\WechatService;

use common\models\Service\WechatModel;
use yii;


class WechatController extends BaseAdminController
{


    /**
     * @return string
     * 微信菜单首页
     */
    public function actionMenu()
    {
        $options = [
            'token' => \Yii::$app->params['token'],
            'encodingaeskey' => \Yii::$app->params['encodingAesKey'],
            'appid' => \Yii::$app->params['appID'],
            'appsecret' => \Yii::$app->params['appSecret'],
            'debug' => true,
        ];

        $wechat = new WechatModel($options);

        $menuJson = $wechat->getMenu();
//        var_dump($menuJson);die;
        if ($menuJson) {
            $menuJson = $menuJson['menu']['button'];
        } else
            $menuJson = [];
        return $this->render('menu', ['menuJson' => $menuJson, 'errCode' => $wechat->errCode]);
    }


    /**
     * @return string
     * 微信菜单改动
     */
    public function actionCreateMenu()
    {
        $post = \Yii::$app->request->post();
        $array = isset($post['content']) ? $post['content'] : '';
        $data = WechatService::CreateMenu($array);

        if ($data['status']) {
            return $this->renderMessage("修改成功", 'wechat/menu');
        } else {
            return $this->renderMessage($data['msg'], 'wechat/menu');
        }
    }

    public function actionSend()
    {
        $model = new NewsPush();
        $dataProvider = new yii\data\ActiveDataProvider([
            'query' => $model->find(),
            'pagination' => [
                'pageSize' => 20,
            ]
        ]);
        return $this->render(
            'send',
            [
                'model' => $model,
                'dataProvider' => $dataProvider,
            ]
        );
    }

    public function actionSendAdd()
    {
        $model = (new NewsPush());
        $post = \Yii::$app->request->post();
        if ($model->load($post)) {
            $model['money'] = intval(floatval($model['money']) * 100);
            $model['startTime'] = strtotime($model['startTime']);
            $model['endTime'] = strtotime($model['endTime']);
            $falg = $model->insert();
            if ($falg) {
                return $this->renderMessage('保存成功!', 'wechat/send', 'success');
            } else {
                return $this->renderMessage('保存失败!' . $model->errors, 'wechat/send', 'danger');
            }
        } else {
            return $this->render('send-add', ['model' => $model]);
        }
    }

    public function actionSendEdit($id)
    {

        $request = Yii::$app->request;
        if ($request->isPost) {
            $Model = (new NewsPush(['scenario' => 'edit']))->findOne($id);
            $post = $request->post();
            if ($Model->load($post)) {
                $Model['money'] = intval(floatval($Model['money']) * 100);
                $Model['startTime'] = strtotime($Model['startTime']);
                $Model['endTime'] = strtotime($Model['endTime']);
                $falg = $Model->save();
                if ($falg) {
                    return $this->renderMessage('保存成功!', 'wechat/send', 'success');
                } else {

                    return $this->renderMessage('保存失败!', 'wechat/send', 'danger');
                }
            }
        } else {
            $model = (new NewsPush(['scenario' => 'edit']))->getOne($id);
            return $this->render('send-edit', ['model' => $model]);
        }
    }


    public function actionSendStatus($id)
    {
        $flag = (new NewsPush(['scenario' => 'status']))->setStatus(intval($id));

        if ($flag['status']) {
            return $this->renderMessage('更改成功！', 'wechat/send');
        } else {
            return $this->renderMessage($flag['msg'], -1);
        }
    }

    /*
     * ActiveForm验证
     */
    public function actionAjaxValidate()
    {
        return $this->ajaxValidate(new NewsPush());
    }

}